package com.synface.utils;

import android.annotation.SuppressLint;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils
{
  private StringUtils()
  {
    throw new AssertionError();
  }

  public static boolean isBlank(String str)
  {
    return (str == null) || (str.trim().length() == 0);
  }

  public static boolean isEmpty(CharSequence str)
  {
    return (str == null) || (str.length() == 0);
  }

  public static boolean isEquals(String actual, String expected)
  {
    return ObjectUtils.isEquals(actual, expected);
  }

  public static int length(CharSequence str)
  {
    return (str == null) ? 0 : str.length();
  }

  public static String nullStrToEmpty(Object str)
  {
    return (str instanceof String) ? (String)str : (str == null) ? "" : str
      .toString();
  }

  public static String capitalizeFirstLetter(String str)
  {
    if (isEmpty(str)) {
      return str;
    }

    char c = str.charAt(0);

    return str
      .length() + 
      Character.toUpperCase(c) + 
      str
      .substring(1);
  }

  public static String utf8Encode(String str)
  {
    if ((!isEmpty(str)) && (str.getBytes().length != str.length())) {
      try {
        return URLEncoder.encode(str, "UTF-8");
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("UnsupportedEncodingException occurred. ", e);
      }
    }

    return str;
  }

  public static String utf8Encode(String str, String defultReturn)
  {
    if ((!isEmpty(str)) && (str.getBytes().length != str.length())) {
      try {
        return URLEncoder.encode(str, "UTF-8");
      } catch (UnsupportedEncodingException e) {
        return defultReturn;
      }
    }
    return str;
  }

  public static String getHrefInnerHtml(String href)
  {
    if (isEmpty(href)) {
      return "";
    }

    String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*";

    Pattern hrefPattern = Pattern.compile(hrefReg, Pattern.CASE_INSENSITIVE);

    Matcher hrefMatcher = hrefPattern.matcher(href);
    if (hrefMatcher.matches()) {
      return hrefMatcher.group(1);
    }
    return href;
  }

  public static String htmlEscapeCharsToString(String source)
  {
    return (isEmpty(source)) ? source : source
      .replaceAll("&lt;", "<")
      .replaceAll("&gt;", ">")
      .replaceAll("&amp;", "&")
      .replaceAll("&quot;", "\"");
  }

  public static String fullWidthToHalfWidth(String s)
  {
    if (isEmpty(s)) {
      return s;
    }

    char[] source = s.toCharArray();
    for (int i = 0; i < source.length; ++i) {
      if (source[i] == '　') {
        source[i] = ' ';
      }
      else if ((source[i] >= 65281) && (source[i] <= 65374))
        source[i] = (char)(source[i] - 65248);
      else {
        source[i] = source[i];
      }
    }
    return new String(source);
  }

  public static String halfWidthToFullWidth(String s)
  {
    if (isEmpty(s)) {
      return s;
    }

    char[] source = s.toCharArray();
    for (int i = 0; i < source.length; ++i) {
      if (source[i] == ' ') {
        source[i] = '　';
      }
      else if ((source[i] >= '!') && (source[i] <= '~'))
        source[i] = (char)(source[i] + 65248);
      else {
        source[i] = source[i];
      }
    }
    return new String(source);
  }

  public static boolean isZero(String s) {
    return isEquals("0", s);
  }

  public static int parseStringToInt(String s) {
    if (isBlank(s)) {
      return 0;
    }
    return Integer.parseInt(s);
  }

  @SuppressLint({"SimpleDateFormat"})
  public static String dateToString(String s)
  {
    if (isBlank(s)) {
      return "";
    }
    s = s.substring(0, 10);
    com.synface.utils.Logs.Logs.releaseLog("date", s);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    Calendar calendar = Calendar.getInstance();
    try {
      date = sdf.parse(s);
      com.synface.utils.Logs.Logs.releaseLog("date", "month:" + date.getMonth() + "");
      calendar.setTime(date);
    } catch (ParseException e) {
      e.printStackTrace();
      return "";
    }

    return calendar.get(Calendar.YEAR) + "年" + (calendar
      .get(Calendar.MONTH) +
      1) + "月" + calendar
      .get(Calendar.DAY_OF_MONTH) +
      "日";
  }

  public static String dateSubString(String s) {
    if (isBlank(s)) {
      return "";
    }
    s = s.substring(0, 10);
    return s;
  }
}